<?php

/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * @see Zend_Gdata_Extension
 */
require_once 'Zend/Gdata/Extension.php';

/**
 * @see Zend_Gdata_Finance
 */
require_once 'Zend/Gdata/Finance.php';

/**
 * @see Zend_Gdata_Finance_Extension_Money
 */
require_once 'Zend/Gdata/Finance/Extension/Money.php';

/**
 * Data model class to represent costBases
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Gdata_Finance_Extension_DaysGain extends Zend_Gdata_Extension
{
	protected $_rootNamespace = 'gf';
	protected $_rootElement = 'daysGain';
	/**
	 * The Money object
	 *
	 * @var Zend_Gdata_Finance_Extension_Money
	 */
	protected $_money = array();

	/**
	 * Constructs a new Zend_Gdata_Finance_Extension_daysGain object
	 * @param string $amount (optional) Amount of money
	 * @param string $currencyCode (optional) ISO4217 currency code
	 */
	public function __construct($amount = null, $currencyCode = null)
	{
		parent::__construct();
	 foreach (Zend_Gdata_Finance::$namespaces as $nsPrefix => $nsUri) {
	 	$this->registerNamespace($nsPrefix, $nsUri);
	 }
	 if ($amount != null and $currencyCode != null)
	 $this->_money[]= new Zend_Gdata_Finance_Extension_Money($amount,$currencyCode);
	}

	/**
	 * Retrieves a DOMElement which corresponds to this element and all
	 * child properties.  This is used to build an entry back into a DOM
	 * and eventually XML text for sending to the server upon updates, or
	 * for application storage/persistence.
	 *
	 * @param DOMDocument $doc The DOMDocument used to construct DOMElements
	 * @return DOMElement The DOMElement representing this element and all
	 * child properties.
	 */
	public function getDOM($doc = null)
	{
		$element = parent::getDOM($doc);
		foreach ($this->_money as $money)
		{
			$element->appendChild($money->getDOM($element->ownerDocument));
		}
		return $element;
	}

	/**
	 * Creates individual Entry objects of the appropriate type and
	 * stores them as members of this entry based upon DOM data.
	 *
	 * @param DOMNode $child The DOMNode to process
	 */
	protected function takeChildFromDOM($child)
	{
		$absoluteNodeName = $child->namespaceURI . ':' . $child->localName;
		switch ($absoluteNodeName)
		{
			case $this->lookupNamespace('gd') . ':' . 'money':
				$money = new Zend_Gdata_Finance_Extension_Money();
				$money->transferFromDOM($child);
				$this->_money[] = $money;
				break;
			default:
				parent::takeChildFromDOM($child);
				break;
		}
	}

	/**
	 * Get the child value for this elements attribute.
	 *
	 * @return  Zend_Gdata_Finance_Extension_Money Object
	 */
	public function getMoney()
	{
		/* This could be multiple objects, for now just return the 1st one */
		return $this->_money[0];
	}

	/**
	 * Set the value for this elements money attribute.
	 *
	 * @param $money  The desired value for this attribute.
	 * @return Zend_Gdata_Finance_Extension_DaysGain.
	 */
	public function setMoney(Zend_Gdata_Finance_Extension_Money $money)
	{
		if ( is_array($money) )
		{
			$this->_money = $money;
			return $this;
		}
		else if ($money instanceof Zend_Gdata_Finance_Extension_Money)
		{
			$this->_money = array($money);
			return $this;
		}
		$e = new Exception("Value needs to be a money object");
		throw $e;
	}
}
